package com.tenbent.product.base.config;

import com.tenbent.product.base.utils.SpringContextUtil;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;

/**
 * 配置contextLoadListener(非webMvc)类
 *
 * @author Randy
 *
 *         Created by ThinkPad on 2017/7/24.
 */
@Configuration
@ComponentScan(basePackages = { "com.tenbent.product" }, excludeFilters = {
		@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class) })
@PropertySources(value = { @PropertySource(value = "classpath:mybatis.properties"),
		@PropertySource(value = "classpath:jdbc.properties"),
		@PropertySource(value = "classpath:security_config.properties"),
		@PropertySource(value = "classpath:redis.properties") })
// @EnableAspectJAutoProxy(proxyTargetClass = true)
public class RootConfig {

	/** 获取属性方式一，通过占位符属性替换方式 ${key} **/
	@Bean
	public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
		PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
		// 设置是否忽略属性文件是否存在，如不不存在抛出异常，设置为true 则不抛出异常。
		placeholderConfigurer.setIgnoreResourceNotFound(false);
		placeholderConfigurer.setFileEncoding("UTF-8");
		return placeholderConfigurer;
	}

	/** 获取属性方式二，通过spring el表达式替换 #{setting['key']} **/
	@Bean(name = "setting")
	public PropertiesFactoryBean propertiesFactoryBean() {
		PropertiesFactoryBean setting = new PropertiesFactoryBean();
		setting.setFileEncoding("UTF-8");
		setting.setIgnoreResourceNotFound(false);
		ResourceLoader resourceLoader = new DefaultResourceLoader();
		Resource mybatisResource = resourceLoader.getResource("classpath:mybatis.properties");
		Resource jdbcResource = resourceLoader.getResource("classpath:jdbc.properties");
		setting.setLocations(jdbcResource, mybatisResource);
		return setting;
	}

	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

	@Bean(name = "springContextUtil")
	public static SpringContextUtil getSpringContextUtil() {
		return new SpringContextUtil();
	}

}
